package com.hq.fxh.No5_User.User.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.GetWebUrlRes;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Model.WebBean;
import com.hq.fxh.R;
import com.hq.fxh.Utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

/**
 * 合作加盟
 */
public class ReXianActivity extends AppCompatActivity {
    private WebView city_partner;
    private List<WebBean> webBean;
    private String buyurl;
    private TextView tvTitle;
    private String title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_partner);
        int uid = SharedPreferenceUtils.getMemberId(ReXianActivity.this);
        webBean = new ArrayList<>();
        final int cityCode = SharedPreferenceUtils.getCityCode(ReXianActivity.this);
        UserServiceImpl.getWebUrl(uid +"",new NetworkSubscriber<GetWebUrlRes>(){
            @Override
            public int onSuccess(GetWebUrlRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    webBean.addAll(data.getData());
                    for (int i = 0; i < webBean.size(); i++) {
                        if (webBean.get(i).getCode().equals("business")) {
                            buyurl = webBean.get(i).getUrl();
                            buyurl = buyurl + "&city=" + cityCode;
                            tvTitle.setText(webBean.get(i).getName());
                            title = webBean.get(i).getName();
                        }
                    }
                    fillHtml(buyurl);
                } else {
                    ToastUtils.show(ReXianActivity.this,data.getMsg());
                }
                return 0;
            }
        });

        city_partner = (WebView) findViewById(R.id.city_partner);
        tvTitle = (TextView)findViewById(R.id.tvTitle);
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        city_partner.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("tel")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(url));
                    startActivity(intent);
                } else {
                    view.loadUrl(url);
                }
                return true;
            }
        });
    }

    private void fillHtml(String url) {
        city_partner.getSettings().setJavaScriptEnabled(true);
        city_partner.getSettings().setSupportZoom(false);
        city_partner.getSettings().setBuiltInZoomControls(false);
        city_partner.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        city_partner.loadUrl(url);
    }

    @Override
    protected void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(this,title);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(this,title);
    }
}
